﻿using System;
using System.Linq;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.Collections.Generic;

public partial class Videoer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (IGameService gameService = ServiceFactory.GameService)
            {
                List<GameVideo> gameVideos = gameService.GetLatestVideos(24).ToList();
                this.uiRepNewestItems.DataSource = gameVideos.Take(4);
                this.uiRepNewestItems.DataBind();

                gameVideos.RemoveAt(0);
                gameVideos.RemoveAt(0);
                gameVideos.RemoveAt(0);
                gameVideos.RemoveAt(0);

                this.uiRepOtherVideos.DataSource = gameVideos;
                this.uiRepOtherVideos.DataBind();
            }
        }
        //HtmlMeta HeaderDescription = new HtmlMeta();
        //HeaderDescription.Name = "description";
        //HeaderDescription.Content = "Videoer, trailer og teaser til spil";
        //this.Header.Controls.Add(HeaderDescription);
    }

    protected void uiRepNewestItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            GameVideo gameVideo = e.Item.DataItem as GameVideo;
            using (IGameService gameService = ServiceFactory.GameService)
            {
                Game game = gameService.GetBy(gameVideo.GameId).FirstOrDefault();
                Image image = e.Item.FindControl("uiNewestVideoImage") as Image;
                GameScreenshot screenshot = gameService.GetAssociatedScreenshots(game.GameId).FirstOrDefault();
                if (screenshot != null)
                {
                    image.ImageUrl = UrlUtillity.GetGamescreenshotImageUri(screenshot);
                }
                Literal title = e.Item.FindControl("uiLitNewestVideoImageTitle") as Literal;
                title.Text = game.Title;
                Literal type = e.Item.FindControl("uiLitNewestVideoType") as Literal;
                type.Text = gameVideo.Title;
            }
        }
    }

    protected void uiRepOtherVideos_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            GameVideo gameVideo = e.Item.DataItem as GameVideo;
            using (IGameService gameService = ServiceFactory.GameService)
            {
                Game game = gameService.GetBy(gameVideo.GameId).FirstOrDefault();
                HyperLink link = e.Item.FindControl("uiHlItem") as HyperLink;

                link.NavigateUrl = UrlUtillity.GetGameVideoUri(gameVideo);
                link.Text = String.Format("{0} - {1}", game.Title, gameVideo.Title);
            }
        }
    }
}
